package com.mxsoft.takeaway.handlers;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

/**
 * MyBatis 设置租户和当前操作人
 *
 * @author zhangyingxuan
 */
@Slf4j
public class MyBatisBasicInfoHandler extends MetaObjectHandler {

    /**
     * <p>
     * 插入元对象字段填充
     * </p>
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        try {
//            UserDto user = SubjectUtils.getUser();
//            if (null != user) {
//                log.info("[{}] insertFill get user #{} ({})", Thread.currentThread().getName(), user.getUserId(), user.getNickName());
//                String tenantId = user.getTenantId();
//                if (null != tenantId) {
//                    setFieldValByName("tenantId", tenantId, metaObject);
//                }
//                Long userId = user.getUserId();
//                if (null != userId) {
//                    setFieldValByName("createBy", String.valueOf(userId), metaObject);
//                    setFieldValByName("updateBy", String.valueOf(userId), metaObject);
//                }
//            } else {
//                log.info("[{}] insertFill can not get user", Thread.currentThread().getName());
//            }
        } catch (Exception e) {
            log.error("Failed to insertFill", e);
        }
    }

    /**
     * 更新元对象字段填充（用于更新时对公共字段的填充）
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        try {
//            UserDto user = SubjectUtils.getUser();
//            if (null != user) {
//                log.info("[{}] updateFill get user #{} ({})", Thread.currentThread().getName(), user.getUserId(), user.getNickName());
//                String tenantId = user.getTenantId();
//                if (null != tenantId) {
//                    setFieldValByName("tenantId", tenantId, metaObject);
//                }
//                Long userId = user.getUserId();
//                if (null != userId) {
//                    setFieldValByName("updateBy", String.valueOf(userId), metaObject);
//                }
//            } else {
//                log.info("[{}] updateFill can not get user", Thread.currentThread().getName());
//            }
        } catch (Exception e) {
            log.error("Failed to updateFill", e);
        }
    }
}
